// pages/myCenter_writeNote/myCenter_writeNote.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    //设置当刷新后，富文本编辑器中的内容呈现上次缓存下来的内容
    cachetimer: null,
    cacheval: null,
    cacheContent: function (d) {
        this.cacheval = d.detail
        if (this.cachetimer) {
            return
        }
        // cachetimer 保存的是每一次的setTimeout的id值
        this.cachetimer = setTimeout(() => {
            // console.log(this.cacheval.html)
            this.cachetimer = null;
            wx.setStorageSync('cache-content', this.cacheval.html)
        }, 1000)

    },
    checkAndSetContent: function () {
        let content = wx.getStorageSync('cache-content')
        // 初始化编辑器内容
        content && this.editor.setContents({
            html: content,
            sucess: () => {
                console.log('ok')
            },
            fail: err => {
                console.log(err)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    editor: null,
    onLoad: function (options) {
        console.log(this.options)
        console.log(this.options.noteContent)
        this.setData({
            noteTitle: this.options.noteTitle,
            noteId: this.options.noteId,
            noteContent: this.options.noteContent
        }) 
        let query = wx.createSelectorQuery()
        query.select('#editor').context(res => {
            //保存实例
            this.editor = res.context;
            if (res) return this.editor.setContents({
                html: this.options.noteContent ? this.options.noteContent : ' ',
                success: function (res) {
                    console.log(res)
                },
                fail: function (err) {
                    console.log(err)
                }
            })
            this.checkAndSetContent()
        }).exec()
    },
    editorPost: function (f) {
        if(this.data.noteId == undefined){
            let {title} = f.detail.value;
            this.editor.getContents({
                success: res => {
                    const token = wx.getStorageSync('token');
                    wx.request({
                        url: `${app.globalData.url}/note`,
                        method: 'POST',
                        dataType: 'json',
                        header: {
                            'content-type': 'text/json',
                            'Authorization': token,
                            'update': 'false'
                        },
                        data: {
                            noteTitle: title,
                            noteContent: res.html
                        },
                        success: res => {
                            // console.log(res);
                            this.setData({
                                title: ""
                            });
                            this.editor.clear()
                            wx.removeStorageSync('cache-content');
                            wx.navigateBack({
                                url: '/pages/myCenter_note/myCenter_note',
                            })
                        },
                        fail: err => {
                            console.log(err);
                        }
                    })
                }
            })
        }else {
            let {title} = f.detail.value;
            this.editor.getContents({
                success: res => {
                    const token = wx.getStorageSync('token');
                    const noteId=this.data.noteId;
                    wx.request({
                        url: `${app.globalData.url}/note`,
                        method: 'POST',
                        dataType: 'json',
                        header: {
                            'content-type': 'text/json',
                            'Authorization': token,
                            'update': 'true'
                        },
                        data: {
                            noteTitle: title,
                            noteContent: res.html,
                            noteId:this.data.noteId
                        },
                        success: res => {
                            // console.log(res);
                            this.setData({
                                title: ""
                            });
                            this.editor.clear()
                            wx.removeStorageSync('cache-content');
                            wx.navigateBack({
                                url: '/pages/myCenter_note/myCenter_note',
                            })
                        },
                        fail: err => {
                            console.log(err);
                        }
                    })
                }
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})